package com.beatingapp;

import java.util.Timer;
import java.util.TimerTask;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.content.Intent;
import android.util.Log;

public class IntroActivity extends SimpleBaseGameActivity 
{

	private final static int CAMERA_WIDTH = 800;

	private final static int CAMERA_HEIGHT = 480;	

	private Camera camera;

	private BitmapTextureAtlas fondoTexture;

	private TextureRegion fondoTextureRegion;

	private int actividad;

	private boolean inicia;

	private int vidas;

	private BitmapTextureAtlas vidasTextureAtlas, sinVidasTextureAtlas;

	private ITextureRegion vidaTextureRegion, sinVidaTextureRegion;

	@Override
	public EngineOptions onCreateEngineOptions() 
	{
		camera = new Camera( 0, 0, CAMERA_WIDTH, CAMERA_HEIGHT );
		EngineOptions engineOptions = new EngineOptions( true, ScreenOrientation.LANDSCAPE_SENSOR,
				new FillResolutionPolicy(), camera );
		return engineOptions;
	}

	@Override
	protected void onCreateResources() 
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath( "gfx/intros/" );
		inicia = true;
		actividad = getIntent( ).getExtras( ).getInt( "Actividad" );
		vidas = getIntent( ).getExtras( ).getInt( "Vidas" );
		String ruta ="";		

		switch ( actividad )
		{
		case 1:
			ruta = "introSonido.jpg";
			break;

		case 2:
			ruta = "introMasaje.jpg";
			break;

		case 3:
			ruta = "introCigarrillo.jpg";
			break;

		default:
			break;
		}

		// cargando el fondo
		this.fondoTexture = new BitmapTextureAtlas( this.getTextureManager( ), 1024, 512 );
		this.fondoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.fondoTexture, this, ruta, 0, 0 );
		this.fondoTexture.load( );

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath( "gfx/vidas/" );

		//vidas
		this.vidasTextureAtlas = new BitmapTextureAtlas( this.getTextureManager( ), 80, 80 );
		this.vidaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.vidasTextureAtlas, this, "vidaG.png", 0, 0 );
		this.vidasTextureAtlas.load( );

		this.sinVidasTextureAtlas = new BitmapTextureAtlas( this.getTextureManager( ), 80, 80 );
		this.sinVidaTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.sinVidasTextureAtlas, this, "sinVidaG.png", 0, 0 );
		this.sinVidasTextureAtlas.load( );

	}

	@Override
	protected Scene onCreateScene() 
	{	

		Scene scene = new Scene( );
		Sprite fondoSprite = new Sprite( 0, 0, fondoTextureRegion, new VertexBufferObjectManager( ) );
		scene.attachChild( fondoSprite );

		Timer t = new Timer();  

		TimerTask task = new TimerTask() {  

			@Override  
			public void run() {  
				runOnUiThread(new Runnable() {  

					@Override  
					public void run() 
					{  		      
						if( inicia )
						{
							try
							{
								Thread.sleep( 3000 );
								Intent intent = null;

								switch ( actividad )
								{
								case 1:
									intent = new Intent(IntroActivity.this, SonidoActivity.class);
									break;

								case 2:
									intent = new Intent(IntroActivity.this, CollisionDetectionExample.class);
									break;

								case 3:
									intent = new Intent(IntroActivity.this, CigarrilloActivity.class);
									break;

								default:
									break;
								}

								intent.putExtras(getIntent().getExtras());
								startActivity(intent);
								inicia = false;
							}
							catch ( InterruptedException e )
							{
								e.printStackTrace();
							}
							IntroActivity.this.finish( );
						}
					}  
				});  
			}  
		};  

		t.scheduleAtFixedRate(task, 0, 1000);

		for (int i = 1; i <= 3; i++) 
		{
			if( vidas >= i )
			{
				Sprite vidas = new Sprite( (80*i) + 190, 100, vidaTextureRegion, new VertexBufferObjectManager( ) );
				scene.attachChild( vidas );
			}
			else
			{
				Sprite vidas = new Sprite( (80*i) + 190, 100, sinVidaTextureRegion, new VertexBufferObjectManager( ) );
				scene.attachChild( vidas );
			}
		}

		return scene;
	}

}
